## merge two bed files, when two regions are overlapped, merge them if they are over X% overlapped, treat them seperately if they are less than X% overlapped.
## author: Yaping Liu  lyping1986@gmail.com
##


my $input_bed_file_1 = $ARGV[0];
my $input_bed_file_2 = $ARGV[1];
my $overlap_threshold = $ARGV[2];

my $use_age = "USAGE: perl mergeBedFilterOverlapByPerOverlap.pl input_bed_file_1 input_bed_file_2 overlap percentage > output_file";
if($#ARGV <2){
	print "$use_age\n";
	exit(1);
}

open(FH,"<$gtf_file_to_check_overlapped") or die;
my @in=<FH>;
chomp(@in);
close(FH);

my %gtf_cor_index=();
foreach my $line(@in){
	##next if $line=~/^\#/;
	my @splitgtf=split "\t",$line;
	#next if $splitvcf[5] <=4;
	my $key = $splitgtf[0]."-".$splitgtf[3]."-".$splitgtf[4];
	$gtf_cor_index{$key} = $line;
}

open(FH,"<$input_gtf_file") or die;
my @gtf=<FH>;
chomp(@gtf);
close(FH);

foreach my $line(@gtf){
	##next if $line=~/^\#/;
	my @splitgtf=split "\t",$line;
	#next if $splitvcf[5] <=4;
	my $key = $splitgtf[0]."-".$splitgtf[3]."-".$splitgtf[4];
	if(exists $gtf_cor_index{$key} ){
		print "$line\n";
	}
}